package cn.brondfish.icedfish.ui.home.setting;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import cn.brondfish.icedfish.framework.config.BF_APPConfigEntity;
import cn.brondfish.icedfish.framework.util.BF_ConfigUtil;

public class AppSettingViewModel extends ViewModel {
    private BF_ConfigUtil util;
    // 配置文件版本
    private MutableLiveData<String> cfg_version;

    // APP设置
    private MutableLiveData<String> app_theme;

    // 用户存储路径
    private MutableLiveData<String> user_storage;

    // 服务器配置
    private MutableLiveData<String> server_host;

    private MutableLiveData<String> server_port;

    private MutableLiveData<String> server_type;

    public AppSettingViewModel(Context context) {
        super();
        init(context);
    }

    private void init(Context context) {
        this.util = BF_ConfigUtil.getInstance(context);
        try {
            PackageInfo package_info = context.getApplicationContext().getPackageManager().
                    getPackageInfo(context.getPackageName(), 0);
            long appVersionCode = package_info.getLongVersionCode();
            BF_APPConfigEntity appConfig = null;
            if(this.util.haveConfig()){
                if(this.util.getAppConfig().getCfg_version() != null && !"".equals(this.util.getAppConfig().getCfg_version())){
                    if(Integer.valueOf(this.util.getAppConfig().getCfg_version())<appVersionCode) {
                        this.util.getAppConfig().setCfg_version(String.valueOf(appVersionCode));
                        this.util.update();
                        appConfig = this.util.getAppConfig();
                    } else {
                        appConfig = this.util.getAppConfig();
                    }
                } else {
                    appConfig = this.util.getAppConfig();
                }
            } else {
                this.util.update();
                appConfig = this.util.getAppConfig();
            }
            if (cfg_version == null){
                cfg_version = new MutableLiveData<>();
            }
            if(app_theme == null) {
                app_theme = new MutableLiveData<>();
            }
            if(user_storage == null) {
                user_storage = new MutableLiveData<>();
            }
            if (server_host == null) {
                server_host = new MutableLiveData<>();
            }
            if(server_port == null) {
                server_port = new MutableLiveData<>();
            }
            if (server_type == null) {
                server_type = new MutableLiveData<>();
            }
            cfg_version.setValue(appConfig.getCfg_version());
            app_theme.setValue(appConfig.getApp_theme());
            server_host.setValue(appConfig.getServer_host());
            server_port.setValue(appConfig.getServer_port());
            server_type.setValue(appConfig.getServer_type());
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public void saveConfig(){
        this.util.getAppConfig().setApp_theme(this.app_theme.getValue());
        this.util.getAppConfig().setServer_host(this.server_host.getValue());
        this.util.getAppConfig().setServer_port(this.server_port.getValue());
        this.util.getAppConfig().setServer_type(this.server_type.getValue());
        this.util.update();
    }

    public MutableLiveData<String> getCfg_version() {
        return cfg_version;
    }

    public MutableLiveData<String> getApp_theme() {
        return app_theme;
    }

    public MutableLiveData<String> getUser_storage() {
        return user_storage;
    }

    public MutableLiveData<String> getServer_host() {
        return server_host;
    }

    public MutableLiveData<String> getServer_port() {
        return server_port;
    }

    public MutableLiveData<String> getServer_type() {
        return server_type;
    }
}
